/*
 * util.h
 *
 *  Created on: 2013-11-6
 *      Author: jzheng
 */
#ifndef __UTIL_H__
#define __UTIL_H__

#include "types.h"
#include <linux/types.h>

#ifdef __LITTLE_ENDIAN
//#error little_endian
#else
#error big_endian
#endif

#ifdef OPTION_DEBUG
int nb_memcmp(void *dest, const void *src, unsigned int count);
#endif

void *memset32(void *s, unsigned int c, unsigned int count);
int checkAllff(unsigned int * buf, unsigned int len);
//int memfind32(unsigned int *s, unsigned int count, unsigned int c);

void disp_pkt(volatile u8 *ptr, u32 len);
void disp_pkt_32(volatile u32 *ptr, u32 len);
void clean_cache_all(void);
void inv_cache_range(const u32 vaddr, const u32 paddr, const u32 size);
u32 get_version_header(void);
u32 get_compile_time(void);



//async memory operate function
//void *async_pageset(void *s, int c, unsigned int mask, int sectsize, int sects, async_cback_fn pcallback, void* param);

#define read_dram_8(ADDR)                       (*(volatile u8 *)(ADDR))
#define read_dram_16(ADDR)                      (*(volatile u16 *)(ADDR))
#define read_dram_32(ADDR)                      (*(volatile u32 *)(ADDR))
#define write_dram_8(ADDR, VAL)                 (*(volatile u8 *)(ADDR) = (VAL))
#define write_dram_16(ADDR, VAL)                (*(volatile u16 *)(ADDR) = (VAL))
#define write_dram_32(ADDR, VAL)                (*(volatile u32 *)(ADDR) = (VAL))
#ifdef __LITTLE_ENDIAN

#define  write_dram_24(ADDR, VAL) \
do \
{ \
    *(u16 *)(ADDR) = (VAL) & 0xffff; \
    *(u8 *)(ADDR + 2) = (VAL) >> 16; \
} while (0)
#else
#define  write_dram_24(ADDR, VAL) \
do \
{ \
    *(u16 *)(ADDR) = (VAL) >> 8; \
    *(u8 *)(ADDR + 2) = (VAL) & 0xff; \
} while (0)

#endif
#define set_bit_dram(BASE_ADDR, BIT_OFFSET)     ((*(volatile u8 *)(BASE_ADDR)) |=  (1 << (BIT_OFFSET)))
#define clr_bit_dram(BASE_ADDR, BIT_OFFSET)     ((*(volatile u8 *)(BASE_ADDR)) &=  (~(1 << (BIT_OFFSET))))
#define tst_bit_dram(BASE_ADDR, BIT_OFFSET)     (((*(volatile u8 *)(BASE_ADDR) & (1 << (BIT_OFFSET)))))

#define set_bit_dram32(BASE_ADDR, BIT_OFFSET)       ((*(volatile u32 *)(BASE_ADDR)) |= (1 << (BIT_OFFSET)))
#define clr_bit_dram32(BASE_ADDR, BIT_OFFSET)       ((*(volatile u32 *)(BASE_ADDR)) &= (~(1 << (BIT_OFFSET))))
#define tst_bit_dram32(BASE_ADDR, BIT_OFFSET)       (*(volatile u32 *)(BASE_ADDR) & (1 << (BIT_OFFSET)))

#define calc_min(x, y) (((x) < (y)) ? (x) : (y))
#define calc_max(x, y) (((x) < (y)) ? (y) : (x))



#define QUEUE_SPACE(size, head, tail)    ( (head) <=  (tail) ?   (size - tail + head - 1) : (head - tail - 1) )
#define QUEUE_COUNT(size, head, tail) ( (head) <= (tail) ? (tail-head) : (size + tail - head) )
#define QUEUE_IS_EMPTY(p)   (p.tail == p.head)
//#define QUEUE_IS_FULL(p)  (QUEUE_SPACE(p.size, p.head, p.tail)==0)

#define ROUND_ADD(ptr, inc, size) ptr = ( (ptr + inc) >= (size) ? (ptr + inc -size) : (ptr + inc) )
#define ROUND_UP(x,y) (((x)+(y)-1)/(y))

#endif /* __UTIL_H__ */
